package com.wlz.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.wlz.listener.TEmployeeExcelListener;
import com.wlz.model.TEmployee;
import com.wlz.service.ITEmployeeService;
import com.wlz.utils.EasyExcelHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

/**
 * @author： wlz
 * @Date 2021/3/1 10:48
 * @description： TODO
 * @version: 1.0
 */

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping(path = "/employee")
public class EmployeeController {

    private final ITEmployeeService employeeService;


    /**
     *  导入
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/import")
    @ResponseBody
    public String importexcel(@RequestParam(value = "excelFile") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), TEmployee.class, new TEmployeeExcelListener(employeeService)).sheet().doRead();
        return "success";
    }

    /**
     *  导出百万条数据 多 sheet
     * @param response
     * @throws Exception
     */
    @GetMapping(path = "/export")
    public void export(HttpServletResponse response) throws Exception {
        long start = System.currentTimeMillis();

        int count = 0;
        EasyExcelHandler handler = null;
        ExcelWriter excelWriter = null;

        try {
            handler = new EasyExcelHandler();
            String fileName = URLEncoder.encode(String.format("%s-(%s).xlsx", "副本社会招聘人员2", UUID.randomUUID().toString()),
                    StandardCharsets.UTF_8.toString());
            excelWriter = handler.create(fileName, TEmployee.class,response,100);

            Integer lastBatchMaxId = 0;
            Integer limit = 900;
            for (; ; ) {
                List<TEmployee> list = employeeService.queryByScrollingPagination(lastBatchMaxId, limit);
                if (list.isEmpty()) {
                    handler.finish(excelWriter);
                    break;
                } else {
                    lastBatchMaxId = list.stream().map(TEmployee::getId).max(Integer::compareTo).orElse(Integer.MAX_VALUE);
                    count += list.size();
                    handler.write(excelWriter,list,count);
                }
            }
        } catch (Exception e) {
            log.error("export 数据导出异常:",e);
        }
        log.info("count:"+ count);
        log.info("导出数据耗时:{} ms,start:{},end:{}", System.currentTimeMillis() - start);
    }



}
